package com.dominator.service.impl;

import com.dominator.enums.ReqEnums;
import com.dominator.mybatis.dao.T_carDao;
import com.dominator.mybatis.dao.T_carPO;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.system.PrimaryGenerater;
import com.dominFramework.core.typewrap.Dto;
import com.dominator.service.CarService;
import com.dominator.utils.api.ApiMessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by czh on 2017/8/25.
 */
@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private T_carDao t_carDao;

    /**
     * 车辆列表
     *
     * @param dto user_id String 必传 用户id
     * @return
     */
    @Override
    public ApiMessage listCars(Dto dto) {
        dto.put("is_valid", "1");
        List<T_carPO> list = t_carDao.list(dto);
        return ApiMessageUtil.success(list);
    }

    /**
     * 添加车辆
     *
     * @param dto user_id String 必传 用户id
     *            car_num String 必传 车牌号
     *            car_vin String 必传 车vin
     *            car_info String 非必传 车辆备注信息
     * @return
     */
    @Override
    @Transactional
    public ApiMessage addCar(Dto dto) throws ApiException {
        try {
            T_carPO car = new T_carPO();
            car.copyProperties(dto);
            car.setId(PrimaryGenerater.getInstance().uuid());
            car.setCreate_time(new Date());
            car.setIs_valid("1");
            int count = 0;
            count = t_carDao.insert(car);
            if (count != 1) {
                throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg());
            }
            return ApiMessageUtil.success(count);
        } catch (ApiException e) {
            e.printStackTrace();
            throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg());
        }
    }

    /**
     * 编辑车辆
     *
     * @param dto car_id String 必传 车辆id
     *            car_num String 必传 车牌号
     *            car_vin String 必传 车vin
     *            car_info String 非必传 车辆备注信息
     * @return
     */
    @Override
    @Transactional
    public ApiMessage editCar(Dto dto) throws ApiException {
        try {
            T_carPO car = new T_carPO();
            car.copyProperties(dto);
            car.setId(dto.getString("car_id"));
            car.setModify_time(new Date());
            int count = 0;
            count = t_carDao.updateByKey(car);
            if (count != 1) {
                throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg());
            }
            return ApiMessageUtil.success();
        } catch (ApiException e) {
            e.printStackTrace();
            throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg());
        }
    }

    /**
     * 删除车辆
     *
     * @param dto car_id String 必传 车辆id
     * @return
     */
    @Override
    @Transactional
    public ApiMessage delCar(Dto dto) throws ApiException {
        try {
            int count = 0;
            count = t_carDao.deleteByKey(dto.getString("car_id"));
            if (count != 1) {
                throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg());
            }
            return ApiMessageUtil.success();
        } catch (ApiException e) {
            e.printStackTrace();
            throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg());
        }
    }
}

